package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.DormRoom;
import com.example.springboot.entity.Student;
import com.example.springboot.entity.SysRole;
import com.example.springboot.entity.SysUserRole;
import com.example.springboot.entity.vo.UserRole;
import com.example.springboot.mapper.StudentMapper;
import com.example.springboot.mapper.SysRoleMapper;
import com.example.springboot.service.StudentService;
import com.example.springboot.service.SysUserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    /**
     * 注入DAO层对象
     */
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SysRoleMapper sysRoleMapper;
    /**
     * 学生登陆
     */
    @Override
    public Student stuLogin(String username, String password) {
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("username", username);
        qw.eq("password", password);
        Student student = studentMapper.selectOne(qw);
        if (student != null) {
            return student;
        } else {
            return null;
        }
    }

    /**
     * 学生新增
     */
    @Override
    public int addNewStudent(Student student) {
        int insert = studentMapper.insert(student);
        return insert;
    }
///////////////////////////////////////
    //admin teacher dorm
    @Override
    public void addStu(Student student) {
        //新增用户
        //保存角色
        int insert=this.baseMapper.insert(student);
        if (insert>0){
            SysUserRole role=new SysUserRole();
            role.setRoleId(student.getRoleId());
            role.setUserId(student.getUserId());
            sysUserRoleService.save(role);
        }
    }

    @Override
    public void updateStu(Student student) {

    }
/////////////////////////////////////////
    /**
     * 分页查询学生
     */
    @Override
    public Page find(Integer pageNum, Integer pageSize, String search) {
        Page page = new Page<>(pageNum, pageSize);
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.like("user_id", search);
        Page studentPage = studentMapper.selectPage(page, qw);

        return studentPage;
    }
    /**
     * 分页查询学生 带角色
     */
    @Override
    public Page findList(Integer pageNum, Integer pageSize, String search) {
        Page page = new Page<>(pageNum, pageSize);
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.like("name", search);
        Page studentPage = studentMapper.selectPage(page, qw);
        // 获取学生列表
        List<Student> students = studentPage.getRecords();
        // 创建一个新的列表用于存储带有角色名称的学生信息
        List<UserRole> userRoles = new ArrayList<>();
        for (Student student : students) {
            // 查询每个学生的角色名称
            QueryWrapper<SysUserRole> query=new QueryWrapper<>();
            query.lambda().eq(SysUserRole::getUserId,student.getUserId());
            SysUserRole one=sysUserRoleService.getOne(query);
            String roleName = "未知角色"; // 默认角色名称
            if (one != null) {
                // 查询角色名称
                SysRole name = sysRoleMapper.selectById(one.getRoleId());
                if (name != null) {
                    roleName = name.getRoleName(); // 更新角色名称
                }
            }
            // 创建新的对象并设置角色名称
            UserRole userRole = new UserRole(student, roleName);
            userRoles.add(userRole);

        }
        // 创建一个新的Page对象用于返回
        Page<UserRole> resultPage = new Page<>(pageNum, pageSize);
        resultPage.setRecords(userRoles);
        resultPage.setTotal(studentPage.getTotal());
        resultPage.setSize(studentPage.getSize());
        resultPage.setCurrent(studentPage.getCurrent());
        return resultPage;
    }

    @Override
    public Student findbuusername(String search) {
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("username", search);
        Student student = studentMapper.selectOne(qw);
        return student;
    }

    /**
     * 更新学生信息
     */
    @Override
    public int updateNewStudent(Student student) {
        int i = studentMapper.updateById(student);
        return i;
    }

    /**
     * 删除学生信息
     */
    @Override
    public int deleteStudent(Long userId) {
        int i = studentMapper.deleteById(userId);
        return i;
    }


    /**
     * 主页顶部：学生统计
     */
    @Override
    public int stuNum() {
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.isNotNull("username");
        int stuNum = Math.toIntExact(studentMapper.selectCount(qw));
        return stuNum;
    }

    /**
     * 床位信息，查询该学生信息
     */
    @Override
    public Student stuInfo(String username) {
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("username", username);
        Student student = studentMapper.selectOne(qw);
        return student;
    }
}
